#pragma once
#include "field.hpp"
#include <memory>
#include <functional>

// 抽象层 将通用功能抽象出来  ，其它具体功能由子类实现
namespace RPC_project
{
    class BaseMessage
    {
    public:
        using ptr = std::shared_ptr<BaseMessage>;

        virtual ~BaseMessage() {};
        virtual void Setid(const std::string &id)
        {
            _rid = id;
        }
        virtual std::string Rid()
        {
            return _rid;
        }

        virtual void SetMyType(const MType mtype)
        {
            _mtype = mtype;
        }
        virtual MType MyType()
        {
            return _mtype;
        }

        virtual std::string Serialize() = 0;
        virtual bool Deserialize(const std::string &msg) = 0; // 反序列化,将其中的数据设置进成员变量
        virtual bool Check() = 0;                             // 检验消息是否完整

    private:
        MType _mtype;
        std::string _rid;
    };

    class BaseBuffer
    {
    public:
        using ptr = std::shared_ptr<BaseBuffer>;
        virtual size_t ReadableBytes() = 0;
        virtual int32_t PeekInt32() = 0;                       // 从缓冲区中取出4个字节 ,不从缓冲区删除
        virtual void RetrieveInt32() = 0;                      // 从缓冲区删除前四个数据
        virtual int32_t ReadInt32() = 0;                       // 从缓冲区中取出4个字节 ,并且从缓冲区将其删除
        virtual std::string RetrieveAsString(size_t size) = 0; // 取出定长字符串
    };

    class BaseProtocol
    {
    public:
        using ptr = std::shared_ptr<BaseProtocol>;
        virtual bool CanProcessed(const BaseBuffer::ptr &ptr) = 0;                     // 消息是否完整
        virtual bool OnMessage(const BaseBuffer::ptr &ptr, BaseMessage::ptr &msg) = 0; // 从buffer中提取消息
        virtual std::string Serialize(const BaseMessage::ptr &ptr) = 0;                // 对消息序列化返回字符串
    };

    class BaseConnection
    {
    public:
        using ptr = std::shared_ptr<BaseConnection>;
        virtual void Send(const BaseMessage::ptr &ptr) = 0; // 用_protocol的接口将消息序列化，再将消息发送？
        virtual void Shutdown() = 0;
        virtual bool Connected() = 0;
    };

    using ConnectionCallback = std::function<void(const BaseConnection::ptr &)>;
    using CloseCallback = std::function<void(const BaseConnection::ptr &)>;
    using MessageCallback = std::function<void(const BaseConnection::ptr &, BaseMessage::ptr &)>;
    class BaseServer
    {
    public:
        using ptr = std::shared_ptr<BaseServer>;
        virtual void SetConnectionCallback(const ConnectionCallback &cb)
        {
            _cb_connection = cb;
        }
        virtual void SetCloseCallback(const CloseCallback &cb)
        {
            _cb_close = cb;
        }
        //消息处理函数(从BaseConnection中读数据body数据(不含LV)(request) ,生成(response) ,发送response)
        virtual void SetMessageCallback(const MessageCallback &cb)
        {
            _cb_message = cb;
        }
        virtual void start() = 0;

    protected:
        ConnectionCallback _cb_connection;
        CloseCallback _cb_close;
        MessageCallback _cb_message;
    };


    class BaseClient
    {
    public:
        using ptr = std::shared_ptr<BaseClient>;
        virtual void SetConnectionCallback(const ConnectionCallback &cb)
        {
            _cb_connection = cb;
        }
        virtual void SetCloseCallback(const CloseCallback &cb)
        {
            _cb_close = cb;
        }
        virtual void SetMessageCallback(const MessageCallback &cb)
        {
            _cb_message = cb;
        }
        virtual void Connect() = 0;//链接服务器
        virtual void Shutdown() = 0;//关闭链接
        virtual bool Send(const BaseMessage::ptr& ) = 0;
        virtual BaseConnection::ptr Connection() = 0;//获取链接
        virtual bool Connected() = 0;//链接是否正常

    protected:
        ConnectionCallback _cb_connection;
        CloseCallback _cb_close;
        MessageCallback _cb_message;
    };
}